﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using YiSha.Entity.RedBookSetManage;
using YiSha.Model.Param.RedBookSetManage;
using YiSha.Util.Extension;
using YiSha.Data.Repository;
using System.Linq;
using YiSha.Util.Model;
using System.Data.Common;
using YiSha.Data;
using YiSha.Util;

namespace YiSha.Service.RedBookSetManage
{
    public class RedBookSetService : RepositoryFactory
    {
        #region 获取数据
        public async Task<List<RedBookSetEntity>> GetList(RedBookSetListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.OrderBy(p => p.Id).ToList();
        }

        public async Task<List<RedBookSetEntity>> GetPageList(RedBookSetListParam param, Pagination pagination)
        {
            var strSql = new StringBuilder();
            List<DbParameter> filter = ListPageFilter(param, strSql);
            pagination.SortType = "desc";
            var list = await this.BaseRepository().FindList<RedBookSetEntity>(strSql.ToString(), filter.ToArray(), pagination);
            return list.ToList();
        }

        public async Task<RedBookSetEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<RedBookSetEntity>(id);
        }

        public async Task<int> GetMaxSort()
        {
            object result = await this.BaseRepository().FindObject("SELECT MAX(id) FROM SysRedBookSet");
            int sort = result.ParseToInt();
            sort++;
            return sort;
        }
        /// <summary>
        /// 获取启动的任务列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<List<RedBookSetEntity>> GetJobList()
        {
            var list = await this.BaseRepository().FindList<RedBookSetEntity>("SELECT * FROM SysRedBookSet  Where SetSwitch = 1");
            return list.ToList();
        }
        #endregion

        #region 提交数据

        public async Task UpdateRedBookSet(RedBookSetEntity entity)
        {
            await this.BaseRepository().Update(entity);
        }

        public async Task SaveForm(RedBookSetEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();
                await this.BaseRepository().Insert<RedBookSetEntity>(entity);
            }
            else
            {
                await entity.Modify();
                await this.BaseRepository().Update<RedBookSetEntity>(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            var db = await this.BaseRepository().BeginTrans();
            try
            {
                long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
                await db.Delete<RedBookSetEntity>(idArr);
                //await db.Delete<>(t => idArr.Contains(t.RedBookSetId.Value));  删除此记录的 consme 消费记录
                await db.CommitTrans();
            }
            catch
            {
                await db.RollbackTrans();
                throw;
            }
        }
        #endregion




        #region 私有方法
        private Expression<Func<RedBookSetEntity, bool>> ListFilter(RedBookSetListParam param)
        {
            var expression = LinqExtensions.True<RedBookSetEntity>();
            if (param != null)
            {
                if (!param.Id.IsEmpty())
                {
                    expression = expression.And(t => t.Id.Equals(param.Id));
                }
            }
            return expression;
        }
        private List<DbParameter> ListPageFilter(RedBookSetListParam param, StringBuilder strSql)
        {
            strSql.Append(@"SELECT  a.*,b.RealName FROM SysRedBookSet a LEFT JOIN SysUser b on a.UserId=b.Id WHERE 1=1 ");

            var parameter = new List<DbParameter>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.name))
                {
                    strSql.Append(" AND a.Name  like @name");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@name", '%' + param.name + '%'));
                }
                if (param.Id > 0)
                {
                    strSql.Append(" AND a.Id = @id");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@id", param.Id));
                }
                if (!string.IsNullOrEmpty(param.link))
                {
                    strSql.Append(" AND a.Link like @link");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@link", '%' + param.link + '%'));
                }
                if (!string.IsNullOrEmpty(param.chuangyio))
                {
                    strSql.Append(" AND a.ChuangYio like @chuangyio");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@chuangyio", '%' + param.chuangyio + '%'));
                }
                if (!string.IsNullOrEmpty(param.chuangyit))
                {
                    strSql.Append(" AND a.ChuangYit like @chuangyit ");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@chuangyit", '%' + param.chuangyit + '%'));
                }
                if (param.budget > 0)
                {
                    strSql.Append(" AND a.Budget = @budget");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@budget", param.budget));
                }
                if (param.currentUserId > 0)
                {
                    strSql.Append(" AND a.Userid = @currentUserId");
                    parameter.Add(DbParameterExtension.CreateDbParameter("@currentUserId", param.currentUserId));
                }
            }
            return parameter;
        }
        #endregion
    }
}
